package com.hwhy.systemmonitoring;

import com.hwhy.systemmonitoring.common.util.ArithUtil;
import org.junit.jupiter.api.Test;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

/**
 * @author LIFULIN
 * @className test
 * @description TODO
 * @date 2020/5/14-14:43
 */
public class test {
    @Test
    public void t1() throws InterruptedException {
        //String hostname = "10.10.1.178";
        String hostname = "10.10.1.174";
        String username = "root";
        //String password = "hwhys178";
        String password = "hwhys174";
        //RemoteComputerMonitorUtil rem = new RemoteComputerMonitorUtil("10.10.1.178", "root", "hwhys178");

        // RemoteComputerMonitorUtil rem = new RemoteComputerMonitorUtil("10.10.1.174", "root", "hwhys174");
        // RemoteComputerMonitorUtil rem = new RemoteComputerMonitorUtil("10.10.1.150",
        // "root", "roots150");
        //LinkedHashMap<Object, Object> m2 =
        //RemoteComputerMonitorUtil.getMemUsage(hostname, username, password);
        //LinkedList<Object> m2 = RemoteComputerMonitorUtil.getDiskUsage(hostname, username, password);
        //String message = JSON.toJSONString(m2);
        //System.out.println(message);
      /*  for (int i = 0; i < 20; i++) {
            RemoteComputerMonitorUtil.getCpuUsage(hostname, username, password);
            Thread.sleep(1000);
        }*/
       /* for (int i = 0; i < 100; i++) {
            LinkedHashMap<String, Object> message = rem.getCpuUsage();
            System.out.println(message);
            Thread.sleep(500);
        }
        int i = rem.serverCheck();
        System.out.println(i);*/

        // LinuxSystemInfoUtil.getAllMessage(hostname, username, password);
        //LinuxSystemInfoUtil.getJvm(hostname, username, password);
    }

    @Test
    public void t2() {
        //返回数组
        LinkedHashMap<Object, Object> map = new LinkedHashMap<>();
        //String s = "%Cpu(s):  8.2 us,  1.0 sy,  0.0 ni, 90.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st,";
        String s = "%Cpu(s):  8.2 us,  1.0 sy,  0.0 ni,90.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st,";
        String news = s.substring(10, 43);
        //根据逗号分割
        String[] spString = news.split(",");
        // System.out.println(news);
        LinkedList<String> linkedList = new LinkedList<>();
        //根据空格分隔
        for (String ss : spString) {
            String[] cpuInfo = ss.split("\\s+");
            //存储到linkedList
            Collections.addAll(linkedList, cpuInfo);
        }
        double used = 0;
        double systotal = 0;
        double free = 0;

        for (int i = 0; i < linkedList.size(); i++) {
            //CPU用户使用率
            used = Double.parseDouble(linkedList.get(0)) + Double.parseDouble(linkedList.get(3));
            //CPU总的使用率
            systotal = used + Double.parseDouble(linkedList.get(6));
            //CPU当前空闲率
            if (linkedList.size() == 11) {
                free = Double.parseDouble(linkedList.get(9));
            } else {
                free = Double.parseDouble(linkedList.get(8));
            }
        }
        map.put("used", used + "%");
        map.put("systotal", systotal + "%");
        map.put("free", free + "%");
        //TODO cpu等待率wait
        map.put("wait", 0.0 + "%");
        System.out.println(map);
    }

    @Test
    public void t3() throws IOException {
        String command = "ipconfig";
        String line = "";
        StringBuilder sb = new StringBuilder();

        // 执行ping命令
        Process process = Runtime.getRuntime().exec(command);

        InputStream inputStream = process.getInputStream();

        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, Charset.forName("GBK"));

        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        try {
            while ((line = bufferedReader.readLine()) != null) {
                sb.append(line).append("\r\n");
            }
        } catch (IOException e) {
            System.out.println("Invalid command.");
        }

        System.out.println(sb.toString());
    }

    @Test
    public void t4() throws InterruptedException {
        //for (int i = 0; i < 200; i++) {
        while (true) {
            SystemInfo si = new SystemInfo();
            HardwareAbstractionLayer hal = si.getHardware();
            //System.out.println(SystemInfoUtil.setCpuInfo(hal.getProcessor()));
            printlnCpuInfo();
            Thread.sleep(2000);
        }


    }

    private static void printlnCpuInfo() throws InterruptedException {
        //System.out.println("----------------cpu信息----------------");
        SystemInfo systemInfo = new SystemInfo();
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 睡眠1s
        TimeUnit.SECONDS.sleep(1);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        System.out.println("----------------cpu信息----------------");
        System.out.println("cpu核数:" + processor.getLogicalProcessorCount());
        System.out.println("cSys:" + cSys);
        System.out.println("totalCpu:" + totalCpu);
        System.out.println("cpu系统使用率:" + ArithUtil.div(totalCpu, cSys, 2));
        System.out.println("cpu系统使用率:" + new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
        System.out.println("cpu用户使用率:" + new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
        System.out.println("cpu当前等待率:" + new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
        System.out.println("cpu当前使用率:" + new DecimalFormat("#.##%").format(1.0 - (idle * 1.0 / totalCpu)));
        System.out.println("cpu当前空闲率:" + new DecimalFormat("#.##%").format(idle * 1.0 / totalCpu));
    }

}
